﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Interop;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Drawing;
using System.Drawing.Imaging;
using System.Drawing.Drawing2D;

namespace VerificationCode
{
    /// <summary>
    /// MainWindow.xaml 的交互逻辑
    /// </summary>
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
        }

        Bitmap SourceCode;
        Bitmap ExtractCode;
        List<Bitmap> SegementChars;
        private void Create_Click(object sender, RoutedEventArgs e)
        {
            //重置全局对象
            SourceCode = null;
            ExtractCode = null;
            SegementChars = null;
            //清空应用界面
            Char1Img.Source = null;
            Char2Img.Source = null;
            Char3Img.Source = null;
            Char4Img.Source = null;
            Hist1Img.Source = null;
            Hist2Img.Source = null;
            Hist3Img.Source = null;
            Hist4Img.Source = null;
            BlackCode.Source = null;
            Char1TxtBox.Text = "";
            Char2TxtBox.Text = "";
            Char3TxtBox.Text = "";
            Char4TxtBox.Text = "";
            LearnBtn.Visibility = System.Windows.Visibility.Hidden;
            Char1CheBox.Visibility = System.Windows.Visibility.Hidden;
            Char1CheBox.IsChecked = false;
            Char2CheBox.Visibility = System.Windows.Visibility.Hidden;
            Char2CheBox.IsChecked = false;
            Char3CheBox.Visibility = System.Windows.Visibility.Hidden;
            Char3CheBox.IsChecked = false;
            Char4CheBox.Visibility = System.Windows.Visibility.Hidden;
            Char4CheBox.IsChecked = false;
            //创建新的验证码
            VCode v1 = new VCode();
            SourceCode = v1.Create(4);
            //显示
            IntPtr back = SourceCode.GetHbitmap(); 
            ColorCode.Source = Imaging.CreateBitmapSourceFromHBitmap(back, IntPtr.Zero, Int32Rect.Empty,BitmapSizeOptions.FromEmptyOptions());
            ////b.Save(".\\VerificationCode.PNG");
        }

        private void Extract_Click(object sender, RoutedEventArgs e)
        {
            if (SourceCode == null)
                return;
            OCR o1 = new OCR();
            ExtractCode = o1.Derived(SourceCode);
            //显示
            IntPtr back = ExtractCode.GetHbitmap();
            BlackCode.Source = Imaging.CreateBitmapSourceFromHBitmap(back, IntPtr.Zero, Int32Rect.Empty, BitmapSizeOptions.FromEmptyOptions());
        }

        private void Segement_Click(object sender, RoutedEventArgs e)
        {
            if (ExtractCode == null)
                return;
            OCR o1 = new OCR();
            SegementChars = o1.Segment(ExtractCode);
            //显示
            IntPtr back1 = SegementChars[0].GetHbitmap();
            Char1Img.Source = Imaging.CreateBitmapSourceFromHBitmap(back1, IntPtr.Zero, Int32Rect.Empty, BitmapSizeOptions.FromEmptyOptions());
            IntPtr back2 = SegementChars[1].GetHbitmap();
            Char2Img.Source = Imaging.CreateBitmapSourceFromHBitmap(back2, IntPtr.Zero, Int32Rect.Empty, BitmapSizeOptions.FromEmptyOptions());
            IntPtr back3 = SegementChars[2].GetHbitmap();
            Char3Img.Source = Imaging.CreateBitmapSourceFromHBitmap(back3, IntPtr.Zero, Int32Rect.Empty, BitmapSizeOptions.FromEmptyOptions());
            IntPtr back4 = SegementChars[3].GetHbitmap();
            Char4Img.Source = Imaging.CreateBitmapSourceFromHBitmap(back4, IntPtr.Zero, Int32Rect.Empty, BitmapSizeOptions.FromEmptyOptions());
        }

        private void ShowHist_Click(object sender, RoutedEventArgs e)
        {
            if (SegementChars == null)
                return;
            List<Bitmap> SegementCharsHist = new List<Bitmap>();
            for (int i = 0; i < SegementChars.Count; i++)
            {
                SegementCharsHist.Add(new Bitmap(SegementChars[i].Width * 2, SegementChars[i].Height * 2));
                SegementCharsHist[i] = Sample.DrawHist(SegementChars[i]);
            }
            //显示
            IntPtr back1 = SegementCharsHist[0].GetHbitmap();
            Hist1Img.Source = Imaging.CreateBitmapSourceFromHBitmap(back1, IntPtr.Zero, Int32Rect.Empty, BitmapSizeOptions.FromEmptyOptions());
            IntPtr back2 = SegementCharsHist[1].GetHbitmap();
            Hist2Img.Source = Imaging.CreateBitmapSourceFromHBitmap(back2, IntPtr.Zero, Int32Rect.Empty, BitmapSizeOptions.FromEmptyOptions());
            IntPtr back3 = SegementCharsHist[2].GetHbitmap();
            Hist3Img.Source = Imaging.CreateBitmapSourceFromHBitmap(back3, IntPtr.Zero, Int32Rect.Empty, BitmapSizeOptions.FromEmptyOptions());
            IntPtr back4 = SegementCharsHist[3].GetHbitmap();
            Hist4Img.Source = Imaging.CreateBitmapSourceFromHBitmap(back4, IntPtr.Zero, Int32Rect.Empty, BitmapSizeOptions.FromEmptyOptions());
        }

        private void Result_Click(object sender, RoutedEventArgs e)
        {
            if (SegementChars == null)
                return;
            Char1TxtBox.Text = Sample.Classify(SegementChars[0]);
            Char2TxtBox.Text = Sample.Classify(SegementChars[1]);
            Char3TxtBox.Text = Sample.Classify(SegementChars[2]);
            Char4TxtBox.Text = Sample.Classify(SegementChars[3]);

            LearnBtn.Visibility = System.Windows.Visibility.Visible;

            Char1CheBox.Visibility = System.Windows.Visibility.Visible;
            Char2CheBox.Visibility = System.Windows.Visibility.Visible;
            Char3CheBox.Visibility = System.Windows.Visibility.Visible;
            Char4CheBox.Visibility = System.Windows.Visibility.Visible;
        }

        private void Learn_Click(object sender, RoutedEventArgs e)
        {
            TextBox charTxtBox = new TextBox();
            int count = 0;
            if (Char1CheBox.IsChecked == true)
            {
                charTxtBox = Char1TxtBox;
                Sample.Create(SegementChars[0], charTxtBox.Text);
                count++;
            }
            if (Char2CheBox.IsChecked == true)
            {
                charTxtBox = Char1TxtBox;
                Sample.Create(SegementChars[1], charTxtBox.Text);
                count++;
            }
            if (Char3CheBox.IsChecked == true)
            {
                charTxtBox = Char1TxtBox;
                Sample.Create(SegementChars[2], charTxtBox.Text);
                count++;
            }
            if (Char4CheBox.IsChecked == true)
            {
                charTxtBox = Char1TxtBox;
                Sample.Create(SegementChars[3], charTxtBox.Text);
                count++;
            }
            if (count == 0)
                return;
            MessageBox.Show("已成功学习" + count + "个样本！","学习成功！");
        }

    }
}
